package com.mairuide._frame.utils.excellistener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.mairuide.idnumber.pojo.IdNumberDetail;
import com.mairuide.idnumber.service.IdNumberDetailService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/8 11:08.
 * 资金管控平台的处理类  针对sheet3 身份信息表
 */
@Component
public class ZJGKPTExcelListener2 extends AnalysisEventListener {
    private IdNumberDetailService idNumberDetailService;
    private List<IdNumberDetail>  data = new ArrayList<IdNumberDetail>();
    private String name="";

    public ZJGKPTExcelListener2(IdNumberDetailService idNumberDetailService) {
        this.idNumberDetailService = idNumberDetailService;
    }

    @Override
    public void invoke(Object object, AnalysisContext context) {
        String pattern = "\\d+";

        List<String> s = (List<String>)object;
        String content = s.get(0);
        if(content==null) return;
        if(content.equals("序号")) return;
        boolean isMatch = Pattern.matches(pattern, content);
        if (!isMatch) return;
        IdNumberDetail idNumberDetail = new IdNumberDetail();
        String name = s.get(1);//姓名
        idNumberDetail.setName(name);
        String IdNumber = s.get(2);//身份证
        idNumberDetail.setIdNumber(IdNumber);
        String serviceAddr = s.get(3);//服务地址，就是银行地址
        idNumberDetail.setServiceAddr(serviceAddr);
        String job = s.get(4);//职业
        idNumberDetail.setjOB(job);
        String home_addr =s.get(5);//家庭住址
        idNumberDetail.setHome_addr(home_addr);
        String nativePlace =s.get(6);//籍贯
        idNumberDetail.setNativePlace(nativePlace);

        if (data.size()>0){
            List<String> collect = data.stream().map(m -> m.getIdNumber()).collect(Collectors.toList());
            if (collect.contains(IdNumber)) return;
        }
        data.add(idNumberDetail);
        if(data.size()>=100){
            doSomething();
            data = new ArrayList<IdNumberDetail>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        doSomething();
    }
    public void doSomething(){
        idNumberDetailService.batchSave(data);
    }
}
